//
//  OtherEmitterAnimationViewController.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/14.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "OtherEmitterAnimationViewController.h"
#import "CommonEmitterView.h"

@interface OtherEmitterAnimationViewController ()

@property (weak, nonatomic) IBOutlet UILabel *commonLabel;

@property (weak, nonatomic) IBOutlet UIButton *commonBtn;
@property (weak, nonatomic) IBOutlet UIButton *commonBtn2;

@property (nonatomic, strong) CommonEmitterView  *emitterview;


@end

@implementation OtherEmitterAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configUI];
}

- (void)configUI {
    [self emitterview];
    self.commonBtn2.hidden = YES;
    self.commonBtn.hidden = YES;
    self.commonLabel.hidden = YES;
    
    self.commonLabel.text = @"点击屏幕试试~ 会有神奇的效果哦~~";
    switch (self.emitterType) {
        case Rain:
            {
                self.emitterview.layer.contents = (id)[[UIImage imageNamed:@"tree.jpg"] CGImage];
                self.commonBtn2.hidden = NO;
                self.commonBtn.hidden = NO;
                [self.emitterview rainLayer];
                [self.commonBtn2 setTitle:@"下小点" forState:UIControlStateNormal];
                [self.commonBtn setTitle:@"下大点" forState:UIControlStateNormal];
            }
            break;
        case Snow:
            {
                self.emitterview.layer.contents = (id)[[UIImage imageNamed:@"tree.jpg"] CGImage];
                [self.emitterview snowLayer];
                
            }
            break;
        case ColorBall:
            {
                self.commonLabel.hidden = NO;
                self.commonLabel.text = @"点击屏幕试试~ 可以改变小球发射位置哦~~";
                [self.emitterview colorBallLayer];
               
            }
            break;
        case LoveHeart:
            {
                self.commonLabel.hidden = NO;
                self.commonLabel.text = @"点击屏幕试试~ 会有小红心跳出来哦~~";
                [self.emitterview heartLayer];
                
            }
            break;
        case Fire:
            {
                self.commonLabel.hidden = NO;
                self.commonLabel.text = @"点击屏幕试试~ 火焰高度可以变化哦~~";
                [self.emitterview smokeLayer];
                
            }
            break;
        case Fireworks:
            {
                [self.emitterview fireworksLayer];
                
            }
            break;
        case QQLike:
            
            break;
        case RedPackage:
            {
                self.commonBtn.hidden = NO;
                [self.commonBtn setTitle:@"红包雨" forState:UIControlStateNormal];
            }
            break;
        default:
            
            break;
    }
}


- (IBAction)commonBtnClick:(UIButton *)sender {
    switch (self.emitterType) {
        case Rain:
            
            [self rainButtonClick:sender];
            break;
        case Snow:
            
            
            break;
        case ColorBall:
            
            break;
        case LoveHeart:
            
            break;
        case Fire:
            
            break;
        case Fireworks:
            
            break;
        case QQLike:
            
            break;
        case RedPackage:
            
            [self redpacketClick:sender];
            break;
        default:
            
            break;
    }
}

#pragma mark - 红包雨
- (void)redpacketClick:(id)sender {
    
    [self.emitterview.redpacketLayer setValue:@1.f forKeyPath:@"birthRate"];
    
    [self performSelector:@selector(endRedpacketAnimation) withObject:nil afterDelay:2.f];
}

- (void)endRedpacketAnimation{
    [self.emitterview.redpacketLayer setValue:@0.f forKeyPath:@"birthRate"];
}

#pragma mark - 下雨
- (void)rainButtonClick:(UIButton *)sender{
    
    NSInteger rate = 1;
    CGFloat scale = 0.05;
    if (sender.tag == 200) {
        
        NSLog(@"下大了");
        
        if (self.emitterview.rainLayer.birthRate < 30) {
            [self.emitterview.rainLayer setValue:@(self.emitterview.rainLayer.birthRate + rate) forKeyPath:@"birthRate"];
            [self.emitterview.rainLayer setValue:@(self.emitterview.rainLayer.scale + scale) forKeyPath:@"scale"];
        }
        
    }else if (sender.tag == 100){
        
        NSLog(@"变小了");
        
        if (self.emitterview.rainLayer.birthRate > 1) {
            [self.emitterview.rainLayer setValue:@(self.emitterview.rainLayer.birthRate - rate) forKeyPath:@"birthRate"];
            [self.emitterview.rainLayer setValue:@(self.emitterview.rainLayer.scale - scale) forKeyPath:@"scale"];
        }
    }
}


#pragma mark - getters
- (CommonEmitterView *)emitterview {
    if (!_emitterview) {
        _emitterview = [[CommonEmitterView alloc] initWithFrame:self.view.bounds];
        [self.view insertSubview:_emitterview atIndex:0];
    }
    return _emitterview;
}

@end
